//
//  SixteenViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "SixteenViewController.h"
#import "Datai4TableViewController.h"
@interface SixteenViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *tabView;
@property(nonatomic,strong) NSMutableArray *dataSouecr;
/**下拉刷新控件*/
@property(nonatomic,strong) UIRefreshControl *refreshControl;
@end

@implementation SixteenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"下拉刷新";
    [self initUIInterface];
    [self initDataSource];
}
/**数据源*/
-(void)initDataSource{
    _dataSouecr = [NSMutableArray arrayWithArray:[UIFont familyNames]];

}
/**界面*/
-(void)initUIInterface{

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Eide" style:UIBarButtonItemStylePlain target:self action:@selector(editButtonTT:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextButtonTTT)];
    
    _tabView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tabView.rowHeight = 80;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    [self.view addSubview:_tabView];
    
    _refreshControl = [[UIRefreshControl alloc]init];
    _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [_refreshControl addTarget:self action:@selector(beginRefresh) forControlEvents:UIControlEventValueChanged];
    [_tabView addSubview:_refreshControl];
}

#pragma mark UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSouecr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *cellIdetifer = @"cellIdetifer";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifer];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifer];
    }
    cell.textLabel.text = _dataSouecr[indexPath.row];
    return cell;
}

/**配置删除确定按钮的title*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
/**提交删除*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        /**删除数据源*/
        [_dataSouecr removeObjectAtIndex:indexPath.row];
        /**删除表格视图*/
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
/**移动表格行*/
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *) sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
   /**获取将要从数据源移动的数据*/
    NSString *willMoveString = [_dataSouecr objectAtIndex:sourceIndexPath.row];
    /**从数据源删除将要移动的数据*/
    [_dataSouecr removeObject:willMoveString];
    /**把移动的数据插入数据源*/
    [_dataSouecr insertObject:willMoveString atIndex:destinationIndexPath.row];
}

#pragma mark method
-(void)nextButtonTTT{
    Datai4TableViewController *datai4 = [[Datai4TableViewController alloc]init];
    [self.navigationController pushViewController:datai4 animated:YES];
}
-(void)editButtonTT:(UIBarButtonItem *)sender{
    [_tabView setEditing:!_tabView.editing animated:YES];
    self.navigationItem.leftBarButtonItem.title = _tabView.editing ? @"Done":@"Edit";
}
-(void)beginRefresh{
  /**2s中停止刷新，然后添加新字体*/
    [self performSelector:@selector(xxxxx) withObject:nil afterDelay:0.2];
}
-(void)xxxxx{
 /**停止刷新*/
    [_refreshControl endRefreshing];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加新的字体" preferredStyle:UIAlertControllerStyleAlert];
    /**为警告框添加一个文本框*/
    [alertController addTextFieldWithConfigurationHandler:nil];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *text = alertController.textFields[0];
        NSString *new  = text.text;
        /**更新数据源，加入新的字体*/
        [_dataSouecr insertObject:new atIndex:0];
        /**插入新的表格行*/
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_tabView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    [alertController addAction:cancel];
    [alertController addAction:sure];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
